package com.hqyj.utils;

import org.junit.Test;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyDateConverter implements Converter<String, Date> {
    @Test
    public void test(){
        System.out.println(convert("2023-09-30"));
    }
    @Override
    public Date convert(String source) {
//        System.out.println(source);
        SimpleDateFormat inputFormat = null;
        if(source.contains("T")){
            inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        }else{
            inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        Date date = null;
        try {
            date = inputFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
